package com.example.cartmanage.chewei.view

import android.util.Log
import android.view.View
import android.widget.EditText
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.cartmanage.R
import com.example.cartmanage.chewei.adapter.CheweiAdapter
import com.example.cartmanage.chewei.adapter.CheweisearchAdapter
import com.example.cartmanage.chewei.model.CheweiViewModel
import com.example.cartmanage.databinding.ActivityCheweiBinding
import com.example.lib_base.mvvm.BaseActivity
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/cartmanage/CheweiActivity")
class CheweiActivity : BaseActivity<ActivityCheweiBinding, CheweiViewModel>() {
    private lateinit var cheweiadapter: CheweiAdapter
    private lateinit var searchAdapter: CheweisearchAdapter
    override fun initView() {
        binding.back.setOnClickListener { finish() }
        binding.add.setOnClickListener {
            ARouter.getInstance().build("/cartmanage/AddCheweiActivity").navigation()
        }
        cheweiadapter = CheweiAdapter()
        searchAdapter = CheweisearchAdapter()
        binding.cheweiRv.adapter = cheweiadapter
        binding.cheweiRv.layoutManager = LinearLayoutManager(this)
        binding.searchRv.adapter = searchAdapter
        binding.searchRv.layoutManager = LinearLayoutManager(this)
        // 默认只显示正常列表
        binding.cheweiRv.visibility = View.VISIBLE
        binding.searchRv.visibility = View.GONE
        vm.cartList(1)

        // 搜索功能
        binding.seach.setOnQueryTextListener(object : android.widget.SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                if (!query.isNullOrEmpty()) {
                    vm.searchChewei(query)
                    vm.searchCheweiType( query)
                    vm.searchCheweiLicense( query)
//                    // 隐藏软键盘
//                    val imm = getSystemService(android.content.Context.INPUT_METHOD_SERVICE) as android.view.inputmethod.InputMethodManager
//                    imm.hideSoftInputFromWindow(binding.seach.windowToken, 0)
                }
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if (newText.isNullOrEmpty()) {
                    // 搜索框清空，恢复正常列表
                    binding.cheweiRv.visibility = View.VISIBLE
                    binding.searchRv.visibility = View.GONE
                    searchAdapter.data.clear()
                    searchAdapter.notifyDataSetChanged()
                }
                return false
            }
        })

        // 左滑删除
        val helper = ItemTouchHelper(object : ItemTouchHelper.Callback() {
            override fun getMovementFlags(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder
            ): Int {
                val left = ItemTouchHelper.LEFT
                return makeMovementFlags(0, left)
            }
            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                return true
            }
            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                val position = viewHolder.layoutPosition
                vm.delChewei(cheweiadapter.data[position].id)
                cheweiadapter.data.removeAt(position)
                cheweiadapter.notifyDataSetChanged()
            }
        })
        helper.attachToRecyclerView(binding.cheweiRv)
        cheweiadapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/cartmanage/DetailCheweiActivity")
                .withInt("cheweiid",cheweiadapter.data[position].id)
                .navigation()
        }
    }

    override fun initData() {

        vm.CartListSuccess.observe(this) { list ->
            // 按id降序排序
            val sortedList = list.sortedByDescending {
                try {
                    when (val t = it.id) {
                        is Int -> t.toLong()
                        else -> 0L
                    }
                } catch (e: Exception) {
                    0L
                }
            }
            cheweiadapter.data.clear()
            cheweiadapter.data.addAll(sortedList)
            cheweiadapter.notifyDataSetChanged()
            // 默认显示正常列表，隐藏搜索结果
            binding.cheweiRv.visibility = View.VISIBLE
            binding.searchRv.visibility = View.GONE
        }
        vm.searchCheweiSuccess.observe(this) {

            searchAdapter.data.clear()
            searchAdapter.data.addAll(it)
            searchAdapter.notifyDataSetChanged()
            // 搜索成功时只显示搜索结果
            binding.cheweiRv.visibility = View.GONE
            binding.searchRv.visibility = View.VISIBLE
        }
//        vm.searchCheweiTypeSuccess.observe(this) {
//            searchAdapter.data.clear()
//            searchAdapter.data.addAll(it)
//            searchAdapter.notifyDataSetChanged()
//            // 搜索成功时只显示搜索结果
//            binding.cheweiRv.visibility = View.GONE
//            binding.searchRv.visibility = View.VISIBLE
//        }
//        vm.searchCheweiLicenseSuccess.observe(this) {
//            searchAdapter.data.clear()
//            searchAdapter.data.addAll(it)
//            searchAdapter.notifyDataSetChanged()
//            // 搜索成功时只显示搜索结果
//            binding.cheweiRv.visibility = View.GONE
//            binding.searchRv.visibility = View.VISIBLE
//        }
        vm.delCheweiSuccess.observe(this) {
            ToastUtils.showShort("删除成功")
        }

    }

    override fun onResume() {
        super.onResume()
        vm.cartList(1)
    }
}